package GUILayer;

import java.awt.EventQueue;
import CtrLayer.*;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuItem;
import ModelLayer.*;
import javax.swing.UIManager;

public class CreateSaleGUI extends JFrame {

	private JFrame frmForetagSalg;
	private JTextField txtFieldBarCode;
	private JTextField txtFieldItemName;
	private JTextField txtFieldAmount;
	private JTextField txtFieldPrice;
	private JTextField txtFieldDiscount;
	private JTextField txtFieldTotalPrice;
	private JTextField textFieldDescription;
	private JTextField txtFieldCustomerId;
	private JTextField txtFieldPhoneNumber;
	private static final long serialVersionUID = 7526472295622776147L;
	private LoginGUI loginGUI;
	private PersonCtr personCtr; 
	private ItemCtr itemCtr;
	private OrderCtr orderCtr;
	private Person customer;
	private Order order;
	private JTextField textFieldOrderDiscount;
	private JTextField textFieldAmount2;
	private JTextField textFieldDiscount2;
	private JTextField textFieldBarCode2;
	private JTextField textFieldEnkelPris;
	private JLabel lblNavn;
	private JTextField textFieldNavn;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CreateSaleGUI window = new CreateSaleGUI();
					window.frmForetagSalg.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CreateSaleGUI() {
		initialize();
		//PersonCtr personCtr = new PersonCtr();
		//OrderCtr orderCtr = new OrderCtr();
		personCtr = personCtr.getInstance();
		orderCtr = orderCtr.getInstance();
		itemCtr = itemCtr.getInstance();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmForetagSalg = new JFrame();
		frmForetagSalg.setTitle("Foretag salg");
		frmForetagSalg.setBounds(100, 100, 607, 719);
		frmForetagSalg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmForetagSalg.setJMenuBar(menuBar);
		
		JMenu mnSalg = new JMenu("File");
		menuBar.add(mnSalg);
		
		JMenuItem mntmAfslutProgram = new JMenuItem("Afslut program");
		mntmAfslutProgram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnSalg.add(mntmAfslutProgram);
		
		JMenu mnHjlp = new JMenu("Hj\u00E6lp");
		menuBar.add(mnHjlp);
		
		JMenuItem om = new JMenuItem("Om");
		om.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAbout();
			}
		});
		mnHjlp.add(om);
		
		
		JPanel panel = new JPanel();
		panel.setBounds(344, 352, 237, 218);
		panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Varebeskrivelse", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(37, 352, 297, 218);
		panel_1.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Foreg\u00E5ende vare og total", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JLabel lblNewLabel_4 = new JLabel("Streg kode:");
		lblNewLabel_4.setBounds(15, 30, 67, 14);
		
		JLabel lblVareNavn = new JLabel("Vare navn:");
		lblVareNavn.setBounds(15, 56, 88, 14);
		
		JLabel lblAntal = new JLabel("Antal:");
		lblAntal.setBounds(15, 105, 67, 14);
		
		JLabel lblPris = new JLabel("Pris:");
		lblPris.setBounds(15, 133, 56, 14);
		
		JLabel lblRabat = new JLabel("Rabat:");
		lblRabat.setBounds(15, 158, 56, 14);
		
		JLabel lblEndeligPris = new JLabel("Total pris:");
		lblEndeligPris.setBounds(15, 186, 90, 14);
		
		txtFieldItemName = new JTextField();
		txtFieldItemName.setBounds(113, 53, 107, 20);
		txtFieldItemName.setColumns(10);
		
		txtFieldPrice = new JTextField();
		txtFieldPrice.setBounds(113, 130, 107, 20);
		txtFieldPrice.setColumns(10);
		
		txtFieldTotalPrice = new JTextField();
		txtFieldTotalPrice.setBounds(113, 183, 107, 20);
		txtFieldTotalPrice.setColumns(10);
		
		JLabel lblKr = new JLabel("Kr.");
		lblKr.setBounds(224, 133, 26, 14);
		
		JLabel label_1 = new JLabel("Kr.");
		label_1.setBounds(226, 186, 26, 14);
		
		textFieldAmount2 = new JTextField();
		textFieldAmount2.setBounds(113, 102, 107, 20);
		textFieldAmount2.setColumns(10);
		
		textFieldDiscount2 = new JTextField();
		textFieldDiscount2.setBounds(113, 156, 107, 20);
		textFieldDiscount2.setColumns(10);
		
		textFieldBarCode2 = new JTextField();
		textFieldBarCode2.setBounds(113, 27, 107, 20);
		textFieldBarCode2.setColumns(10);
		
		textFieldDescription = new JTextField();
		textFieldDescription.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(textFieldDescription, GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(textFieldDescription, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(37, 581, 297, 74);
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "F\u00E6rdigg\u00F8r salg", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBounds(37, 11, 297, 171);
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Kunde", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JLabel lblKundeId = new JLabel("Kunde ID:");
		
		JLabel lblTelefonnummer = new JLabel("Telefonnr.:");
		
		txtFieldCustomerId = new JTextField();
		txtFieldCustomerId.setColumns(10);
		
		txtFieldPhoneNumber = new JTextField();
		txtFieldPhoneNumber.setColumns(10);
		
		JButton btnGodkend = new JButton("Godkend");
		btnGodkend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
				createOrder();
				textFieldNavn.setText(customer.getName());
				}
				catch(Exception e){
					JOptionPane.showMessageDialog(frmForetagSalg, "Udfyld alle felter eller, \n" + "tjek om felterne er udfyldt korrekt \n" + "eller tjek om kunden findes i systemet", "Error Message", JOptionPane.ERROR_MESSAGE);
			}
			}
		});
		
		JButton btnRydOrder = new JButton("Ryd");
		btnRydOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearOrder();
				
			}
		});
		
		JButton button_2 = new JButton("Accepter salg");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new ReceiptGUI();
				
			}
		});
		
		JButton btnTilbage = new JButton("Tilbage");
		btnTilbage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmForetagSalg.dispose();
			}
		});
		frmForetagSalg.getContentPane().setLayout(null);
		frmForetagSalg.getContentPane().add(panel_1);
		panel_1.setLayout(null);
		panel_1.add(lblNewLabel_4);
		panel_1.add(lblVareNavn);
		panel_1.add(lblAntal);
		panel_1.add(lblPris);
		panel_1.add(lblRabat);
		panel_1.add(txtFieldPrice);
		panel_1.add(lblKr);
		panel_1.add(textFieldAmount2);
		panel_1.add(txtFieldItemName);
		panel_1.add(textFieldDiscount2);
		panel_1.add(txtFieldTotalPrice);
		panel_1.add(label_1);
		panel_1.add(textFieldBarCode2);
		panel_1.add(lblEndeligPris);
		
		JLabel label_2 = new JLabel("%");
		label_2.setBounds(224, 158, 14, 14);
		panel_1.add(label_2);
		
		JLabel lblPrisPrStk = new JLabel("Enkel pris:");
		lblPrisPrStk.setBounds(15, 80, 88, 14);
		panel_1.add(lblPrisPrStk);
		
		textFieldEnkelPris = new JTextField();
		textFieldEnkelPris.setBounds(113, 77, 107, 20);
		panel_1.add(textFieldEnkelPris);
		textFieldEnkelPris.setColumns(10);
		
		JLabel label_7 = new JLabel("Kr.");
		label_7.setBounds(224, 80, 26, 14);
		panel_1.add(label_7);
		frmForetagSalg.getContentPane().add(panel_2);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(6)
					.addComponent(button_2)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnTilbage)
					.addGap(111))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_2)
						.addComponent(btnTilbage)))
		);
		panel_2.setLayout(gl_panel_2);
		frmForetagSalg.getContentPane().add(panel_3);
		
		JLabel lblRabtPOrdre = new JLabel("Rabat p\u00E5 ordre:");
		
		textFieldOrderDiscount = new JTextField();
		textFieldOrderDiscount.setColumns(10);
		
		JLabel label = new JLabel("%");
		
		textFieldNavn = new JTextField();
		textFieldNavn.setEditable(false);
		textFieldNavn.setColumns(10);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_3.createSequentialGroup()
							.addGap(10)
							.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_3.createSequentialGroup()
									.addComponent(lblKundeId, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
									.addGap(31)
									.addComponent(txtFieldCustomerId, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_3.createSequentialGroup()
									.addComponent(lblTelefonnummer, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addGap(4)
									.addComponent(txtFieldPhoneNumber, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_3.createSequentialGroup()
									.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
										.addComponent(lblRabtPOrdre, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE)
										.addGroup(gl_panel_3.createSequentialGroup()
											.addGap(100)
											.addComponent(textFieldOrderDiscount, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)))
									.addGap(4)
									.addComponent(label, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_panel_3.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_3.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(textFieldNavn, Alignment.LEADING)
								.addGroup(Alignment.LEADING, gl_panel_3.createSequentialGroup()
									.addComponent(btnGodkend, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addGap(4)
									.addComponent(btnRydOrder)))))
					.addContainerGap(54, Short.MAX_VALUE))
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_3.createSequentialGroup()
					.addGap(11)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addComponent(lblKundeId)
						.addComponent(txtFieldCustomerId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(6)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_3.createSequentialGroup()
							.addGap(3)
							.addComponent(lblTelefonnummer))
						.addComponent(txtFieldPhoneNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(5)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.LEADING)
						.addComponent(textFieldOrderDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_3.createSequentialGroup()
							.addGap(3)
							.addComponent(lblRabtPOrdre))
						.addGroup(gl_panel_3.createSequentialGroup()
							.addGap(3)
							.addComponent(label)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(textFieldNavn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_3.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnGodkend)
						.addComponent(btnRydOrder))
					.addContainerGap())
		);
		panel_3.setLayout(gl_panel_3);
		frmForetagSalg.getContentPane().add(panel);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Foretag salg", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_4.setBounds(37, 193, 297, 148);
		frmForetagSalg.getContentPane().add(panel_4);
		
		JButton btnGodkendPartOrder = new JButton("Godkend");
		btnGodkendPartOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
				addPartOrder();
				clearPartOrder();
				showPreviousPartOrder();
				}
				catch(Exception e2){
					JOptionPane.showMessageDialog(frmForetagSalg, "Udfyld alle felter eller, \n" + "tjek om felterne er udfyldt korrekt \n" + "eller tjek om varen findes i systemet", "Error Message", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		JButton btnRydPartOrder = new JButton("Ryd");
		btnRydPartOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearPartOrder();
			}
		});
		
		txtFieldBarCode = new JTextField();
		txtFieldBarCode.setColumns(10);
		
		txtFieldAmount = new JTextField();
		txtFieldAmount.setColumns(10);
		
		txtFieldDiscount = new JTextField();
		txtFieldDiscount.setColumns(10);
		
		JLabel label_3 = new JLabel("%");
		
		JLabel label_4 = new JLabel("Streg kode:");
		
		JLabel label_5 = new JLabel("Antal:");
		
		JLabel label_6 = new JLabel("Rabat:");
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(txtFieldBarCode, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(txtFieldAmount, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(label_6, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(txtFieldDiscount, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
							.addGap(4)
							.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel_4.createSequentialGroup()
							.addComponent(btnGodkendPartOrder, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnRydPartOrder)))
					.addContainerGap(51, Short.MAX_VALUE))
		);
		gl_panel_4.setVerticalGroup(
			gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_4.createSequentialGroup()
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(3)
							.addComponent(label_4))
						.addComponent(txtFieldBarCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(6)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(3)
							.addComponent(label_5))
						.addComponent(txtFieldAmount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(3)
							.addComponent(label_6))
						.addComponent(txtFieldDiscount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_4.createSequentialGroup()
							.addGap(3)
							.addComponent(label_3)))
					.addGap(18)
					.addGroup(gl_panel_4.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnGodkendPartOrder)
						.addComponent(btnRydPartOrder))
					.addContainerGap())
		);
		panel_4.setLayout(gl_panel_4);
		frmForetagSalg.setVisible(true);
	}
	private void createOrder()
	{
		
		double discount = Double.parseDouble(textFieldOrderDiscount.getText());
		Person employee = personCtr.getLogOn();
		order = orderCtr.createOrder(discount, employee, findCustomer());
		
	}
	
	private void showAbout()
	{
		JOptionPane.showMessageDialog(frmForetagSalg, "Vestbjerg Byggecenter\n" + "Version 1.0", "About Vestbjerg Byggecenter", JOptionPane.INFORMATION_MESSAGE);
	}
	
	
	public Person getCustomer()
	{
		return customer;
	}

	
	private Person findCustomer()
	{
		String phoneNumber = txtFieldPhoneNumber.getText();
		int customerId = Integer.parseInt(txtFieldCustomerId.getText());
	    customer = personCtr.findCustomer(phoneNumber, customerId);
	    return customer;
	}
	
	private void addPartOrder()
	{
	  String barCode = txtFieldBarCode.getText();
	  int quantity = Integer.parseInt(txtFieldAmount.getText());
	  double discount = Double.parseDouble(txtFieldDiscount.getText());
	  itemCtr.scanBarCode(barCode, quantity, discount, order);
	}
	
	private void clearOrder()
	{
		txtFieldPhoneNumber.setText("");
		txtFieldCustomerId.setText("");
		textFieldOrderDiscount.setText("");
		
	}
	
	private void clearPartOrder()
	{
		txtFieldBarCode.setText("");
		txtFieldAmount.setText("");
		txtFieldDiscount.setText("");
		
	}
	
	private void showPreviousPartOrder()
	{
		PartOrder partOrder = itemCtr.getPartOrder();
		Item item = partOrder.getItem();
		double totalDiscount = partOrder.getDiscount() + item.getDiscount() + order.getDiscount();
		textFieldBarCode2.setText(item.getBarCode());
		txtFieldItemName.setText(item.getItemName());
		textFieldEnkelPris.setText(""+item.getRetailPrice());
		textFieldAmount2.setText(""+ partOrder.getQuantity());
		txtFieldPrice.setText(""+item.getRetailPrice() * partOrder.getQuantity());
		textFieldDiscount2.setText(""+totalDiscount);	
		textFieldDescription.setText(item.getDescription());
		txtFieldTotalPrice.setText(""+order.getTotalPrice());
	}
}